package com.wanlian.dao.impl;

import com.wanlian.common.PageCondition;
import com.wanlian.dao.RoleGroupAuthorityDao;
import com.wanlian.entity.Authority;
import com.wanlian.entity.OrgRoleGroup;
import com.wanlian.entity.RoleGroupAuthority;
import com.wanlian.model.ResultModel;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class RoleGroupAuthorityDaoImpl extends GenericDaoImpl<RoleGroupAuthority> implements RoleGroupAuthorityDao {
    public RoleGroupAuthorityDaoImpl() {
        super(RoleGroupAuthority.class);
    }

    @Override
    public RoleGroupAuthority queryByPd(int authorityId, int groupId) {
        String hql = String.format("from %s where authorityid=? and groupid=? ", entityClassName());
        RoleGroupAuthority roleGroupAuthority = this.queryForObject(hql, new Object[]{authorityId, groupId});
        return roleGroupAuthority;
    }


    @Override
    public RoleGroupAuthority queryByAuthorityId(int authorityId) {
        String hql = String.format("from %s where authorityid=?  ", entityClassName());
        RoleGroupAuthority roleGroupAuthority = this.queryForObject(hql, new Object[]{authorityId});
        return roleGroupAuthority;
    }

    @Override
    public List<RoleGroupAuthority> queryByGid(int groupid) {
        String hql = String.format("from %s where groupid=?  ", entityClassName());
        List<RoleGroupAuthority> list = this.queryForList(hql, new Object[]{groupid});
        return list;
    }


    @Override
    public ResultModel queryByParams(OrgRoleGroup orgRoleGroup) {
        ResultModel resultModel=new ResultModel(true);
        //根据用户组id查询其权限
        List<RoleGroupAuthority> lists = new ArrayList<RoleGroupAuthority>();
        String hql = String.format("from %s where  groupid=? and isenable=true ", entityClassName());
        lists = this.queryForList(hql, new Object[]{orgRoleGroup.getRoleGroupId()});
        resultModel.setObject(lists);
        return resultModel;
    }

    @Override
    public RoleGroupAuthority queryByGAid(int groupId, int authorityId) {
        String hql = String.format("from %s where authorityid=? and groupid=? and isenable=true  ", entityClassName());
        RoleGroupAuthority roleGroupAuthority = this.queryForObject(hql, new Object[]{authorityId,groupId});
        return roleGroupAuthority;
    }
}
